package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.TbFaultLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.TbFaultLogs;
import com.ruoyi.system.service.ITbFaultLogsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 故障记录Controller
 * 
 * @author ruoyi
 * @date 2023-07-07
 */
@RestController
@RequestMapping("/logs")
public class TbFaultLogsController extends BaseController
{
    @Autowired
    private ITbFaultLogsService tbFaultLogsService;

    /**
     * 查询故障记录列表
     */
    @RequiresPermissions("system:logs:list")
    @GetMapping("/list")
    public TableDataInfo list(TbFaultLogs tbFaultLogs)
    {
        startPage();
        List<TbFaultLogs> list = tbFaultLogsService.selectTbFaultLogsList(tbFaultLogs);
        return getDataTable(list);
    }

    /**
     * 导出故障记录列表
     */
    @RequiresPermissions("system:logs:export")
    @Log(title = "故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbFaultLogs tbFaultLogs)
    {
        List<TbFaultLogs> list = tbFaultLogsService.selectTbFaultLogsList(tbFaultLogs);
        ExcelUtil<TbFaultLogs> util = new ExcelUtil<TbFaultLogs>(TbFaultLogs.class);
        util.exportExcel(response, list, "故障记录数据");
    }

    /**
     * 获取故障记录详细信息
     */
    @RequiresPermissions("system:logs:query")
    @GetMapping(value = "/{faultIds}")
    public AjaxResult getInfo(@PathVariable("faultIds") Long faultIds)
    {
        return success(tbFaultLogsService.selectTbFaultLogsByFaultIds(faultIds));
    }

    /**
     * 新增故障记录
     */
    @RequiresPermissions("system:logs:add")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbFaultLogs tbFaultLogs)
    {
        return toAjax(tbFaultLogsService.insertTbFaultLogs(tbFaultLogs));
    }

    /**
     * 修改故障记录
     */
    @RequiresPermissions("system:logs:edit")
    @Log(title = "故障记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbFaultLogs tbFaultLogs)
    {
        return toAjax(tbFaultLogsService.updateTbFaultLogs(tbFaultLogs));
    }

    /**
     * 删除故障记录
     */
    @RequiresPermissions("system:logs:remove")
    @Log(title = "故障记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{faultIdss}")
    public AjaxResult remove(@PathVariable Long[] faultIdss)
    {
        return toAjax(tbFaultLogsService.deleteTbFaultLogsByFaultIdss(faultIdss));
    }


    /**
     * 添加
     */
    @RequiresPermissions("system:logs:sendLog")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/{msg}")
    public AjaxResult sendLog(@PathVariable("msg") String msg)
    {
        TbFaultLogs tbFaults=new TbFaultLogs();
        System.out.println(msg);
        String flag=msg.substring(0,msg.indexOf(","));
        String vin=msg.substring(msg.indexOf(",")+1);

        List<TbFaultLogs> tbFaultLogs = tbFaultLogsService.selectTbFaultLogsList(tbFaults);
        for (TbFaultLogs tbFaultLog : tbFaultLogs) {
            if (tbFaultLog.getCarVin().equals(vin) && tbFaultLog.getFaultName().equals(flag)){
                return null;
            }
        }

        TbFaultLogs tbFaultLogs1=new TbFaultLogs();
        tbFaultLogs1.setCarVin(vin);
        tbFaultLogs1.setFaultName(flag);
        return toAjax(tbFaultLogsService.insertTbFaultLogs(tbFaultLogs1));
    }



}
